//
//  HepatitisVirusViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/26.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "HepatitisVirusViewController.h"
#import "SuifangSubitemViewCell.h"

@interface HepatitisVirusViewController () <UITextFieldDelegate>
@property (nonatomic,strong) NSArray *rowUIArr;
@end

@implementation HepatitisVirusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"肝炎病毒指标";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(saveAction)];
    UIImage *img = [[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:img
                                                                            style:UIBarButtonItemStyleDone
                                                                           target:self
                                                                           action:@selector(backAction)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.allowsSelection = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SuifangSubitemViewCell"
                                               bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"cell"];
    
    self.rowUIArr = @[
                      @{@1:@"乙肝表面抗体", @2:@"参考值:0-10ml U/ml", @3:@"IU/L"},
                      @{@1:@"乙肝e抗体", @2:@"参考值:阴性/阳性", @3:@""},
                      @{@1:@"乙肝核心抗体", @2:@"参考值:阴性/阳性", @3:@""},
                      @{@1:@"乙肝表面抗原", @2:@"参考值:阴性/阳性", @3:@""},
                      @{@1:@"乙肝DNA", @2:@"参考值:<5x102 copies/ml", @3:@"IU/L"},
                      @{@1:@"丙肝RNA", @2:@"参考值:<103 copies/ml", @3:@"IU/L"},
                      ];
    if (!_hepatitisVirus) {
        self.hepatitisVirus = [RPHepatitisVirusBean new];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 事件响应
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveAction{
    if ([self.delegate respondsToSelector:@selector(backHepatitisVirus:)]) {
        [self.delegate backHepatitisVirus:self.hepatitisVirus];
    }
    [self backAction];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SuifangSubitemViewCell staticHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rowUIArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *rowUIData = _rowUIArr[indexPath.row];
    SuifangSubitemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SuifangSubitemViewCell alloc]init];
    }
    [cell renderUIWithText:rowUIData];
    cell.valTextfield.tag = indexPath.row+1;
    cell.valTextfield.delegate = self;
    cell.valTextfield.text = [self.hepatitisVirus valueForKey:[NSString stringWithFormat:@"hepatitis%ld",(long)indexPath.row+1]];
    return cell;
}

- (RPHepatitisVirusBean *)hepatitisVirus{
    if (!_hepatitisVirus) {
        _hepatitisVirus = [RPHepatitisVirusBean new];
    }
    return _hepatitisVirus;
}

#pragma mark UITextField代理
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (textField.text.length>0) {
        NSString *key = [NSString stringWithFormat:@"hepatitis%ld", (long)textField.tag];
        [self.hepatitisVirus setValue:textField.text forKey:key];
    }
    return YES;
}

@end
